#include "junix.h"

int main(int argc, char **argv)
{
  char objfile[1024];

  FILE *fp=popen("ls -t ./*.cc","r");
  if(!fp)
    err_quit("popen error\n");

  if( fgets(objfile, 1024, fp)==NULL )
    {
      if( pclose(fp) == -1 )
	err_quit("pclose error");

      err_quit("fgets error");
    }

  int ret=pclose(fp);
  if(ret != 0)
    err_quit("No cc file");

  char cmd[1024]={'\0'};
  strcpy(cmd,"g++ -g -ggdb -Wall ");
  strcat(cmd,objfile);

  assert( cmd[strlen(cmd)-1]=='\n' );
  if( cmd[strlen(cmd)-1]=='\n' )
    cmd[ strlen(cmd)-1 ] = '\0';

  fprintf(stdout, "%s\n", cmd);
  fflush(stdout);

  ret=system(cmd);
  if(ret!=0)
    err_quit("compile error");

  return 0;
}

